package com.shengsheng.expschedule.service.expschedule.controller.admin;


import com.shengsheng.expschedule.base.result.R;
import com.shengsheng.expschedule.service.expschedule.entity.Semester;
import com.shengsheng.expschedule.service.expschedule.service.ScheduleService;
import com.shengsheng.expschedule.service.expschedule.service.SemesterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 学期表 前端控制器
 * </p>
 *
 * @author yuan
 * @since 2021-09-29
 */
@RestController
@RequestMapping("/admin/expschedule/semester")
@Api(tags = "学期管理")
@RefreshScope
public class SemesterController {

    @Autowired
    private SemesterService semesterService;
    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation("增加学期信息")
    @PostMapping("/save")
    public R saveSemester(@ApiParam(value = "学期对象", required = true)
                          @RequestBody Semester semester) {

        if (semesterService.save(semester)) return R.ok().message("添加成功");
        return R.error().message("添加失败");
    }

    @ApiOperation("删除学期信息")
    @DeleteMapping("/remove/{id}")
    public R removeById(@ApiParam(value = "学期 Id",required = true)
                        @PathVariable String id) {

        // 此 ID 已在 Schedule 表引用，无法删除
        if (scheduleService.isExistSemesterId(id)) return R.error().message("此学期已在已添加课程，无法删除");
        if (semesterService.removeById(id)) return R.ok().message("删除成功");
        return R.error().message("数据不存在");
    }

    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("修改学期信息")
    @PostMapping("/update")
    public R updateSemester(@ApiParam(value = "学期对象",required = true)
                            @RequestBody Semester semester) {

        // 计算修改后的学期初始日期之差
        Long betweenDays = semesterService.CalcBetweenDays(semester.getId(), semester.getSemesterOriginDate());
        // 同步修改 Schedule 表中的 date 数据
        scheduleService.updateDate(betweenDays);
        if (semesterService.updateById(semester)) return R.ok().message("修改成功");
        return R.error().message("数据不存在");
    }

    @ApiOperation("查询所有学期信息")
    @GetMapping("/get")
    public R getSemesterList() {
        return R.ok()
                .data("items",semesterService.getSemesterListOrderByOriginDate())
                .message("获取数据成功");
    }

    @ApiOperation("根据 Id 查询学期信息")
    @GetMapping("/get/{id}")
    public R getSemesterById(@ApiParam(value = "学期 Id",required = true)
                             @PathVariable String id) {
        Semester semester = semesterService.getById(id);
        if (semester == null) return R.error().message("数据不存在");
        return R.ok()
                .data("item", semester)
                .message("获取数据成功");
    }

    @ApiOperation("根据 Id 设置当前学期")
    @PostMapping("/set/{id}")
    public R setCurrentSemesterById(@ApiParam(value = "学期 Id",required = true)
                                    @PathVariable String id) {
        if (semesterService.setCurrentSemesterById(id)) return R.ok().message("设置成功");
        return R.error().message("设置失败");

    }
}

